package fr.n7.khome.view.editors;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellEditor;

import fr.n7.khome.core.Fuzzy;
import fr.n7.khome.core.Request;
import fr.n7.khome.util.FuzzyFormatException;
import fr.n7.khome.util.InvalidFuzzyValueException;
import fr.n7.khome.view.JFuzzy;
import fr.n7.khome.view.KHomeResultTableModel;


public class JFuzzyEditor extends AbstractCellEditor implements TableCellEditor, KeyListener, MouseListener, ChangeListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -656400050944122021L;
	private JPanel totalPanel;
	private JTextField textField;
	private JFuzzy jFuzzy;
	private Fuzzy value;
	private KHomeResultTableModel<List<Request>> resultTableModel;
	
	private int row;
	private int col;
	
	public JFuzzyEditor(KHomeResultTableModel<List<Request>> resultTableModel){
		super();
		this.resultTableModel = resultTableModel;
		textField = new JTextField("?");
		textField.setHorizontalAlignment(JTextField.CENTER);
		textField.selectAll();
		textField.setBackground(new Color(250,250,250));
		value = Fuzzy.whateverFuzzy();
		textField.addKeyListener(this);
		
		jFuzzy = new JFuzzy(true);
		
		totalPanel = new JPanel(new BorderLayout());
		totalPanel.add(textField, BorderLayout.SOUTH);
		totalPanel.add(jFuzzy, BorderLayout.CENTER);
		totalPanel.addMouseListener(this);
		jFuzzy.addMouseListener(this);
		jFuzzy.addChangeListener(this);
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		if(value instanceof Fuzzy) {
			this.row = row;
			this.col = column;
			this.value = (Fuzzy)value;
			textField.setText(value.toString());
			textField.selectAll();
			jFuzzy.setFuzzy(this.value, false);
		}
		return totalPanel;
	}
	
	@Override
	public boolean stopCellEditing(){
		Fuzzy f = null;
		String stringFuzzy = textField.getText();
		if(stringFuzzy.equals("")){
			textField.setText(value.toString());
			return false;
		}else{
			try {
				f = Fuzzy.parse(stringFuzzy);
			} catch (FuzzyFormatException e) {
				textField.setText(value.toString());
				return false;
			} catch (InvalidFuzzyValueException e) {
				textField.setText(value.toString());
				return false;
			}
		}
		value = f;
		textField.setText(value.toString());
		fireEditingStopped();
		return true;
	}
	
	@Override
	public Object getCellEditorValue() {
		return value;
	}

	@Override
	public void keyPressed(KeyEvent arg0) {}

	@Override
	public void keyReleased(KeyEvent arg0) {
		Fuzzy f = null;
		try {
			f = Fuzzy.parse(textField.getText());
		} catch (FuzzyFormatException e) {
		} catch (InvalidFuzzyValueException e) {
		} catch (NumberFormatException e){
		}
		if(f == null){
			textField.setForeground(Color.red);
		}else if(!f.equals(jFuzzy.getFuzzy())){
			textField.setForeground(Color.black);
			jFuzzy.setFuzzy(f, false);
			updateResults(f);
		}
	}
	
	protected void updateResults(Fuzzy f){
		resultTableModel.updateResultsWithData(row, col, f);
	}

	@Override
	public void keyTyped(KeyEvent arg0) {}

	@Override
	public void mouseClicked(MouseEvent arg0) {}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		textField.requestFocus();
	}

	public void stateChanged(ChangeEvent e) {
		Fuzzy f = ((JFuzzy)e.getSource()).getFuzzy();
		textField.setText(f.toString());
		updateResults(f);
	}

}
